define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'shopro/goods/xg/index' + location.search,
                    add_url: 'shopro/goods/xg/add',
                    edit_url: 'shopro/goods/xg/edit',
                    del_url: 'shopro/goods/xg/del',
                    multi_url: 'shopro/goods/xg/multi',
                    import_url: 'shopro/goods/xg/import',
                    table: 'shopro_goods_xg',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'goods_id', title: '商品ID',operate:false},
                        {field: 'goods_name', title: '商品名称',operate:false},
                        {field: 'store_id', title: '商户ID',operate:false},
                        {field: 'store_name', title: '商户名称',operate:false},
                        {field: 'user_id', title: '用户ID',operate:false},
                        {field: 'user_name', title: '用户名称',operate:false},
                        {field: 'content', title: __('Content'),operate:false,
                            formatter : function(value, row, index, field){
                                return "<span style='display: block;overflow: hidden;text-overflow: ellipsis;white-space: nowrap;' title='" + row.content + "'>" + value + "</span>";
                            },
                            cellStyle : function(value, row, index, field){
                                return {
                                    css: {
                                        "white-space": "nowrap",
                                        "text-overflow": "ellipsis",
                                        "overflow": "hidden",
                                        "max-width":"300px"
                                    }
                                };
                            }},
                        {field: 'status', title: '状态',searchList: {"0":'待修改',"1":'已修改'}, formatter: Table.api.formatter.normal,operate:false},
                        {field: 'createtime', title: __('Createtime'),operate:false, addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'),operate:false, addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.buttons,
                            buttons: [

                                {
                                    name: 'ajax',
                                    text: '已修改',
                                    title: '已修改',
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: '',
                                    hidden:function (row){
                                        return false;
                                    },
                                    disable:function (row){
                                        if (row.status==1){
                                            return true;
                                        }
                                    },
                                    url: 'shopro/goods/xg/collection_status/type/1',
                                    confirm:function(row){
                                        return '确定已经修改该申请吗';
                                    },
                                    success: function (data, ret) {
                                        Layer.alert(ret.msg);
                                        table.bootstrapTable('refresh');
                                        //如果需要阻止成功提示，则必须使用return false;
                                        //return false;
                                    },
                                    error: function (data, ret) {
                                        Layer.alert(ret.msg);
                                        return false;
                                    }
                                },
                                {
                                    name: 'ajax',
                                    text: '未修改',
                                    title: '未修改',
                                    classname: 'btn btn-xs btn-success btn-ajax btn-restoreit',
                                    icon: '',
                                    hidden:function (row){
                                        return false;
                                    },
                                    disable:function (row){
                                        if (row.status==2){
                                            return true;
                                        }
                                    },
                                    url: 'shopro/goods/xg/collection_status/type/0',
                                    confirm:function(row){
                                        return '确定恢复至未修改状态吗';
                                    },
                                    success: function (data, ret) {
                                        Layer.alert(ret.msg);
                                        table.bootstrapTable('refresh');
                                        //如果需要阻止成功提示，则必须使用return false;
                                        //return false;
                                    },
                                    error: function (data, ret) {
                                        Layer.alert(ret.msg);
                                        return false;
                                    }
                                },

                            ]
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
